package com.bjpowernode.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.query.ActivityQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.ActivityService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;


/**
 * 市场活动
 * 
 */
@RequestMapping("/api")
@RestController
public class ActivityController {

	@Autowired
	private ActivityService activityService;

	@GetMapping("/activitys")
	public R queryActivityPage(@RequestParam(value = "current", required = false) Integer current,
			ActivityQuery activityQuery) {

		if (current == null || current == 0) {
			current = 1;
		}

		return R.OK(activityService.getActivitysByPage(current, activityQuery));

	}
	
	/**
	 * 添加市场活动
	 * 
	 * @param activityQuery
	 * @return 响应结果 1成功 其他 失败
	 */
	@PostMapping("/activity/add")
	public R saveActivity(ActivityQuery activityQuery, @RequestHeader(value = Constants.TOKEN_NAME) String token) {
		activityQuery.setToken(token);
		int result = activityService.saveActivity(activityQuery);
		return result >= 1 ? R.OK() : R.FAIL();
	}
	
}
